<?php

class HotelController extends CController
{
	public function actionIndex()
	{
		$this->setPageTitle('Отели');
		
		$this->render('index');
	}
	
	public function actionGetdata()
	{
		$start = (intval($_GET['start']) >= 0) ? intval($_GET['start']) : 0;
		$limit = (intval($_GET['limit']) >= 0) ? intval($_GET['limit']) : 20;
		$sort  = in_array(strtolower($_GET['sort']), array('id', 'hoteltitle', 'citytitle', 'stars')) ? strtolower($_GET['sort']) : 'hoteltitle'; 
		$dir   = in_array(strtolower($_GET['dir']), array('asc', 'desc')) ? strtolower($_GET['dir']) : 'asc';
		$callback = $_GET['callback'];

		$records = Hotel::model()->criteria($sort, $dir, $start, $limit)->with(array('city'))->findAll();
		$count   = Hotel::model()->count();
		
		$this->renderPartial('json', array(
			'data'  => $records,
			'count' => $count,
			'callbackFunction' => $callback
		), false, true);
	}
	
	public function actionCreate()
	{
		$this->setPageTitle('Отели');
		
		$imagesConfig = Hotel::getImagesConfig();
		
		$excursions    = CActiveRecordData::getArray(
			Excursion::model()->criteria('title', 'asc')->findAll(),
			array('id', 'title'), false
		);
		
		$hotelFeatures = CActiveRecordData::getArray(
			HotelFeature::model()->criteria('title', 'asc')->findAll(),
			array('id', 'title'), false
		);
		
		$roomFeatures  = CActiveRecordData::getArray(
			RoomFeature::model()->criteria('title', 'asc')->findAll(),
			array('id', 'title'), false
		);
		
		$cities        = CActiveRecordData::getArray(
			City::model()->criteria('title', 'asc')->findAll(),
			array('id', 'title'), false
		);
		
		$this->render('create', array(
			'imagesConfig'  => $imagesConfig,
			'excursions'    => $excursions,
			'hotelFeatures' => $hotelFeatures,
			'roomFeatures'  => $roomFeatures,
			'cities' => $cities 
		));
	}
	
	public function actionCreatehandler()
	{
		$hotel = new Hotel();
		
		if (isset($_POST['Hotel'])) {
			$hotel->attributes = $_POST['Hotel'];
			$hotel->datetime   = date('Y-m-d H-i-s');
			
			if ($hotel->save()) {
				$hotel->saveExcursions($hotel->id, $_POST['Hotel2excursion']);
				$hotel->saveHotelFeatures($hotel->id, $_POST['Hotel2hotelFeatures']);
				$hotel->saveRoomFeatures($hotel->id, $_POST['Hotel2roomFeatures']);
				$hotel->uploadPhotos($hotel->id);
				
				FlashMessage::saveInfo(array(
					'title'  => 'Инфо',
					'text'   => 'Гостиница сохранена'
				));
            }
		}
		
		$this->redirect('/admin/hotel/');
	}
	
	public function actionEdit()
	{
		$this->setPageTitle('Отели');
		
		$imagesConfig = Hotel::getImagesConfig();
		
		$id = intval($_GET['id']);
	
		if ($id > 0) {
			$hotel = Hotel::model()->with(
				array('excursions',
					  'hotelFeatures',
					  'roomFeatures',
					  'hotelPhotos')
			)->findByPk($id);
			
			$excursions    = CActiveRecordData::getArray(
				Excursion::model()->criteria('title', 'asc')->findAll(),
				array('id', 'title')
			);
		
			$hotelFeatures = CActiveRecordData::getArray(
				HotelFeature::model()->criteria('title', 'asc')->findAll(),
				array('id', 'title')
			);
		
			$roomFeatures  = CActiveRecordData::getArray(
				RoomFeature::model()->criteria('title', 'asc')->findAll(),
				array('id', 'title')
			);
		
			$cities        = CActiveRecordData::getArray(
				City::model()->criteria('title', 'asc')->findAll(),
				array('id', 'title'), false
			);
			
			$currentHotelExcursions  = CActiveRecordData::getList($hotel->getRelated('excursions'), 'excursionId');
			$currentHotelFeaturesIds = CActiveRecordData::getList($hotel->getRelated('hotelFeatures'), 'hotelFeatureId');
			$currentRoomFeaturesIds  = CActiveRecordData::getList($hotel->getRelated('roomFeatures'), 'roomFeatureId');
			
			$selectedExcursions    = array();
			$notSelectedExcursions = array();
			
			foreach($excursions as $excursion) {
				if (in_array($excursion['id'], $currentHotelExcursions)) {
					$selectedExcursions[] = array_values($excursion);
				} else {
					$notSelectedExcursions[] = array_values($excursion);
				}
			}
			
			$selectedHotelFeatures    = array();
			$notSelectedHotelFeatures = array();
			
			foreach($hotelFeatures as $feature) {
				if (in_array($feature['id'], $currentHotelFeaturesIds)) {
					$selectedHotelFeatures[] = array_values($feature);
				} else {
					$notSelectedHotelFeatures[] = array_values($feature);
				}
			}
		
			$selectedRoomFeatures    = array();
			$notSelectedRoomFeatures = array();
			
			foreach($roomFeatures as $feature) {
				if (in_array($feature['id'], $currentRoomFeaturesIds)) {
					$selectedRoomFeatures[] = array_values($feature);
				} else {
					$notSelectedRoomFeatures[] = array_values($feature);
				}
			}

			$this->render('edit', array(
				'hotel'  => $hotel,
				'cities' => $cities,
				'imagesConfig' => $imagesConfig,
				
				'hotelPhotos' => $hotel->getRelated('hotelPhotos'),
				
				'excursions'           => $excursions,
				'selectedExcursions'   => $selectedExcursions,
				'notSelectedExcursions'=> $notSelectedExcursions,
			
				'hotelFeatures'            => $hotelFeatures,
				'selectedHotelFeatures'    => $selectedHotelFeatures,
				'notSelectedHotelFeatures' => $notSelectedHotelFeatures,
			
				'roomFeatures'            => $roomFeatures,
				'selectedRoomFeatures'    => $selectedRoomFeatures,
				'notSelectedRoomFeatures' => $notSelectedRoomFeatures 
			));
		}
	}
	
	public function actionEdithandler()
	{
		$hotelId = intval($_GET['id']);
		
		$hotel = new Hotel();
		
		if (isset($_POST['Hotel'])) {
			
			if (Hotel::model()->findByPk($hotelId)) {
				
				if (!array_key_exists('availableTours', $_POST['Hotel'])) {
					$_POST['Hotel']['availableTours'] = 0;
				}
				
				Hotel::model()->updateByPk($hotelId, $_POST['Hotel']);
				$hotel->saveExcursions($hotelId, $_POST['Hotel2excursion']);
				$hotel->saveHotelFeatures($hotelId, $_POST['Hotel2hotelFeatures']);
				$hotel->saveRoomFeatures($hotelId, $_POST['Hotel2roomFeatures']);
				$hotel->reloadPhotos(
					$hotelId,
					((array_key_exists('HotelPhotoReload', $_POST) && is_array($_POST['HotelPhotoReload'])) ? array_keys($_POST['HotelPhotoReload']) : array()),
					((array_key_exists('HotelPhotoRemove', $_POST) && is_array($_POST['HotelPhotoRemove'])) ? $_POST['HotelPhotoRemove'] : array())
				);
				
				FlashMessage::saveInfo(array(
					'title'  => 'Инфо',
					'text'   => 'Гостиница сохранена'
				));
            }
		}
		
		$this->redirect('/admin/hotel/');
	}
	
	public function actionRemove()
	{
		$id = intval($_GET['id']);

		if ($id > 0) {
			$hotel = Hotel::model()->findByPk($id);
			
			if ($hotel) {
				$hotel->removeAllConnections($id);
				$hotel->delete();
			}
		}
		
		die();
	}
}